/*
 * CChatWorld.h
 *
 *  Created on: 2012-7-16
 *      Author: guanlei
 */

#ifndef CCHATWORLD_H_
#define CCHATWORLD_H_
#include "Message.h"

#include "IChater.h"
#include "IChannel.h"
#include "CChannel.h"

class CChatWorld {
public:
	CChatWorld();
	virtual ~CChatWorld();
	void InitChatWorld();
	void SendMessageToWorld(UserChatMsg*);
	void SendMessageToChannel(UserChatMsg*,CHANNEL_ID);
	bool EnterChatWorld(IChater*);
	void LeftChatWorld(CHATER_ID);
	void DumpAllChannelInfo(char* buffer,int max_size);
	void SetGmChaterId(CHATER_ID gm_id){
		_gm_chater_id = gm_id;
	}

	CHATER_ID GetGmChaterId(){
		return _gm_chater_id;
	}
	
	IChannel*	CreateChannel(CHANNEL_ID,const char*);

	IChater*	FindChaterByChaterId(CHATER_ID	chater_id){
		std::map<CHATER_ID,IChater*>::iterator itr = _all_chaters.find(chater_id);
		if(itr != _all_chaters.end()){
			return itr->second;
		}
		return NULL;
	}

	IChannel*	FindChannelByChannelId(CHANNEL_ID channel_id){
		if(channel_id == WORLD_CHANNEL_ID){
			return _world_channel;
		}

		std::map<CHANNEL_ID,IChannel*>::iterator itr = _all_channels.find(channel_id);
		if(itr != _all_channels.end()){
			return itr->second;
		}
		return NULL;
	}

private:
	//世界频道
	IChannel*			_world_channel;
	CHATER_ID			_gm_chater_id;
	std::map<int,IChannel*> _all_channels;
	std::map<int,IChater*>	_all_chaters;
	omg::MutexLock		_lock;
};


class CChatWorldManager{
public:
	typedef std::map<WORLD_ID,CChatWorld*>::iterator WORLD_ITR;
	typedef std::map<WORLD_ID,CChatWorld*> WORLD_MAP;

	static CChatWorld* GetWorld(WORLD_ID	world_id){
		if(_m_instance == NULL){
			_m_instance = new CChatWorldManager();			
		}	
				
		WORLD_ITR itr = _m_world_map.find(world_id);
		if(itr != _m_world_map.end()){
			return itr->second;		
		}
		return NULL;
	}

	static CChatWorldManager* GetInstance(){
		if(_m_instance == NULL){
			_m_instance = new CChatWorldManager();
		}
		return _m_instance;
	}

	void RegisterWorld(WORLD_ID world){
		_m_world_map[world] = new CChatWorld();		
	}

private:
	CChatWorldManager(){
	}

private:
	static CChatWorldManager*	_m_instance;
	static std::map<WORLD_ID,CChatWorld*>	_m_world_map;


};


#endif /* CCHATWORLD_H_ */
